package com.baijia.dao.impl;

import com.baijia.dao.ThirdLoginAccountDao;
import com.baijia.dao.po.Passport;
import com.baijia.dao.po.ThirdLoginAccount;
import com.baijia.util.JdbcUtil;
import com.mysql.jdbc.Statement;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by dudu on 16/7/13.
 */
@Repository
public class ThirdLoginAccountDaoImpl extends CommonDaoImpl<ThirdLoginAccount, Long> implements ThirdLoginAccountDao {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private String selectSql =
            "select id,uid,passport_id as passportId, nickname, channel,create_time as createTime,update_time as updateTime from cdb.passport_third_account ";

    public ThirdLoginAccountDaoImpl() {
        super(ThirdLoginAccount.class);
    }

    @Override
    protected String selectSql() {
        return selectSql;
    }

    @Override
    public ThirdLoginAccount getByUid(String uid, String channel) {
        logger.info("getByUid params uid = {}, channel = {} ", uid, channel);
        List<ThirdLoginAccount> list = jdbcTemplate.query(selectSql + " where uid=? and channel=?",
                new BeanPropertyRowMapper<>(ThirdLoginAccount.class), new Object[]{uid, channel});
        if(list != null && list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    String INSERT_SQL = "insert into cdb.passport_third_account(uid,channel,nickname,create_time) values(?,?,?,?) ";

    @Override
    public long save(final ThirdLoginAccount thirdLoginAccount) {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
                int index = 0;
                JdbcUtil.setString(ps, ++index, thirdLoginAccount.getUid());
                JdbcUtil.setString(ps, ++index, thirdLoginAccount.getChannel());
                JdbcUtil.setString(ps, ++index, thirdLoginAccount.getNickName());
                JdbcUtil.setTimestamp(ps, ++index, new Date());
                return ps;
            }
        },keyHolder);
        long autoIncId = keyHolder.getKey().longValue();
        return autoIncId;
    }

    @Override
    public int updatePassportId(Long passportId, String thirdId) {
        return jdbcTemplate.update("update cdb.passport_third_account set passport_id = ? where id = ?",
                new Object[]{passportId, thirdId});
    }
}
